﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class CellImages : Window, IWindow
    {
        DataTemplate imageCellDataTemplate;

        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }
  
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public CellImages()
        {
            InitializeComponent();

            dataGrid.ColumnsHierarchy.AllowResize = false;
            dataGrid.RowsHierarchy.AllowResize = false;
            this.imageCellDataTemplate = this.Resources["imageCellDataTemplate"] as DataTemplate;

            LoadGridData();
        }

        private void LoadGridData()
        {
            for (int i = 0; i < carInfo.Length; i++)
            {
                string[] parts = carInfo[i].Split(';');
                listCars.Add(new Car(parts[0], parts[1], parts[2], parts[3], parts[4]));
            }

            dataGrid.ItemsSource = listCars;
            dataGrid.ColumnsHierarchy.Items[1].CellDataTemplate = this.imageCellDataTemplate;

            for (int i = 0; i < dataGrid.RowsHierarchy.Items.Count; i++)
                dataGrid.RowsHierarchy.Items[i].Height = 80.0;

            for (int i = 0; i < dataGrid.ColumnsHierarchy.Items.Count; i++)
            {
                dataGrid.ColumnsHierarchy.Items[i].Width = 133.0;
                dataGrid.ColumnsHierarchy.Items[i].CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;            
            }

            dataGrid.ColumnsHierarchy.Items[4].Width = 150.0;

            dataGrid.RowsHierarchy.Visible = false;

            dataGrid.Refresh();
        }

        List<Car> listCars = new List<Car>();

        static string[] carInfo = new string[] 
                {
                    "Porsche Boxster;images/porsche.png;4 years;www.porsche.com;$47,600 - $57,500",
                    "Audi A4;images/audia4.png;4 years;www.audi.com;$31,000 - $48,800",
                    "BMW 656Ci;images/bmw6.png;4 years;www.bmw.com;$78,700 - $86,100",
                    "BMW 760Li;images/bmw7.png;4 years;www.bmw.com;$136,500 - $153,300",
                    "Mercedes SL550;images/mercedes.png;4 years;www.mercedes.com;$98,600 - $110,500",
                };

        public class Car
        {
            public Car(string make, string imageUrl, string warranty, string website, string msrp)
            {
                this.Image = imageUrl;
                this.Make = make;
                this.Warranty = warranty;
                this.Website = website;
                this.MSRP = msrp;
            }

            public string Make
            {
                get;
                set;
            }

            public string Image
            {
                get;
                set;
            }

            public string Warranty
            {
                get;
                set;
            }

            public string MSRP
            {
                get;
                set;
            }

            public string Website
            {
                get;
                set;
            }
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            this.listCars.Clear();
            this.listCars = null;
            base.OnClosing(e);
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
  
    }

}
